package com.datang.yourtomcat02;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Locale;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-09-16 18:15
 * @notify 相应对象
 * @version 1.0
 */
public class Response implements ServletResponse {
    private static final int BUFFER_SIZE = 1024;
    Request request;
    OutputStream output;
    PrintWriter writer;

    public Response(OutputStream output) {
        this.output = output;
    }


    /*
     * @author 顶风少年
     * @date 2019/9/16 22:06
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param request
     * @result void
     * @notify 设置请求对象
     * version 1.0
     */
    public void setRequest(Request request) {
        this.request = request;
    }


    /*
     * @author 顶风少年
     * @date 2019/9/16 22:07
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result void
     * @notify 相应静态资源
     * version 1.0
     */
    public void sendStaticResource() throws IOException {

        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        try {
            //尝试创建资源对象
            File file = new File(Constants.WEB_ROOT, request.getUri());
            //创建流对象
            fis = new FileInputStream(file);
            //将资源读入字节数组
            int ch = fis.read(bytes, 0, BUFFER_SIZE);

            while (ch != -1) {
                //写入相应
                output.write(bytes, 0, ch);
                ch = fis.read(bytes, 0, BUFFER_SIZE);
            }
        } catch (FileNotFoundException e) {
            //没有对应资源抛出异常页面
            String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: 23\r\n" +
                    "\r\n" +
                    "<h1>File Not Found</h1>";
            output.write(errorMessage.getBytes());
        } finally {
            //关闭流
            if (fis != null) {
                fis.close();
            }
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/9/16 22:15
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.io.PrintWriter
     * @notify 获取写入流
     * version 1.0
     */
    public PrintWriter getWriter() throws IOException {
        writer = new PrintWriter(output, true);
        return writer;
    }

    public String getCharacterEncoding() {
        return null;
    }

    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }



    public void setContentLength(int i) {

    }

    public void setContentType(String s) {

    }

    public void setBufferSize(int i) {

    }

    public int getBufferSize() {
        return 0;
    }

    public void flushBuffer() throws IOException {

    }

    public void resetBuffer() {

    }

    public boolean isCommitted() {
        return false;
    }

    public void reset() {

    }

    public void setLocale(Locale locale) {

    }

    public Locale getLocale() {
        return null;
    }
}
